import 'package:enterprise_v6_app/app/components/custom_refresh.dart';
import 'package:enterprise_v6_app/app/components/education/train_plan/custom_evalute_input_page.dart';
import 'package:enterprise_v6_app/app/core/core.dart';
import 'package:enterprise_v6_app/app/core/ext/ext.dart';
import 'package:enterprise_v6_app/app/data/entities/application/education/couser_center/education_comment_list_entity.dart';
import 'package:enterprise_v6_app/app/modules/application/education/course_center/integrated_theme/course_detail_detail_page/course_detail/evaluate/evaluate_controller.dart';

///评价
class EvaluatePage extends StatelessWidget {
  EvaluatePage({super.key});

  final controller = Get.put(EvaluateController());

  @override
  Widget build(BuildContext context) {
    return GetBuilder(
        init: controller,
        builder: (_) {
          return Scaffold(
            resizeToAvoidBottomInset: false,
            key: controller.riskRightScaffoldKey,
            body: Container(
              color: Colors.white,
              padding: EdgeInsets.only(top: 12.w, right: 16.w, left: 16.w, bottom: 50.w),
              child: Column(
                children: [
                  _topWidget(),
                  //评论
                  _bottomWidget(),
                ],
              ),
            ), // 右侧筛选
          );
        });
  }

  Widget _rowData(EducationCommentListEntity item, String name, String value, {Alignment alignment = Alignment.centerLeft}) {
    return Row(
      children: [
        Text(
          name,
          style: TextStyle(
            color: const Color(0xFFA5A6B5),
            fontWeight: FontWeight.w400,
            fontSize: 13.sp,
          ),
        ),
        Expanded(
            child: Align(
          alignment: alignment,
          child: Row(
            mainAxisSize: MainAxisSize.min,
            children: [
              SvgPicture.asset(AppImages.icon_train_like_),
              AppGaps.hGap8,
              Text(
                value,
                style: TextStyle(
                  color: const Color(0xFF495061),
                  fontSize: 12.sp,
                  fontWeight: FontWeight.w400,
                ),
              )
            ],
          ),
        ).clickOnce(() {
          controller.onClickTrainLike(item);
        })),
      ],
    );
  }

  Widget itemWidget(EducationCommentListEntity item) {
    return Container(
      padding: EdgeInsets.all(12.w),
      decoration: BoxDecoration(
        color: Colors.white,
        borderRadius: BorderRadius.circular(8.w),
      ),
      child: Row(
        children: [
          SvgPicture.asset(
            AppImages.icon_train_teacher_bg,
          ),
          AppGaps.hGap8,
          Expanded(
              child: Column(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: [
              Row(
                children: [
                  Text(
                    item.commentUserName ?? '',
                    style: TextStyle(
                      color: const Color(0xFF495061),
                      fontWeight: FontWeight.w400,
                      fontSize: 14.sp,
                    ),
                  ),
                  Row(
                    children: [
                      _commentScore(image: item.commentScore! >= 1 ? AppImages.icon_train_plan_evaluate : AppImages.icon_train_plan_evaluate_default),
                      _commentScore(image: item.commentScore! >= 2 ? AppImages.icon_train_plan_evaluate : AppImages.icon_train_plan_evaluate_default),
                      _commentScore(image: item.commentScore! >= 3 ? AppImages.icon_train_plan_evaluate : AppImages.icon_train_plan_evaluate_default),
                      _commentScore(image: item.commentScore! >= 4 ? AppImages.icon_train_plan_evaluate : AppImages.icon_train_plan_evaluate_default),
                      _commentScore(image: item.commentScore! == 5 ? AppImages.icon_train_plan_evaluate : AppImages.icon_train_plan_evaluate_default),
                    ],
                  ),
                ],
              ),
              AppGaps.vGap6,
              _rowData(item, item.createDate ?? '', '${item.commentLike ?? ''}', alignment: Alignment.centerRight),
              AppGaps.vGap6,
              Text(
                item.commentContent ?? '',
                style: TextStyle(
                  color: const Color(0xFF495061),
                  fontWeight: FontWeight.w400,
                  fontSize: 14.sp,
                ),
              ),
            ],
          ))
        ],
      ),
    ).click(() {
      // controller.courseDetail();
    });
  }

  Widget _commentScore({String image = AppImages.icon_train_plan_evaluate_default}) {
    return Container(
      width: 24,
      height: 24,
      padding: EdgeInsets.all(5.w),
      child: SvgPicture.asset(image),
    );
  }

  Widget _topWidget() {
    return Column(
      children: [
        //评价
        Row(
          children: [
            Obx(() => Row(
                  children: controller.evaluate
                      .map((e) => Container(
                            padding: EdgeInsets.all(5.w),
                            child: SvgPicture.asset(e.click.value ? AppImages.icon_train_plan_evaluate : AppImages.icon_train_plan_evaluate_default),
                          ).clickOnce(() {
                            //点击了多少个
                            for (var evaluate in controller.evaluate) {
                              if (controller.evaluate.indexOf(evaluate) > controller.evaluate.indexOf(e)) {
                                //遍历中的对象大于点击的对象就全部灰暗
                                evaluate.click.value = false;
                              } else {
                                //当前点击对象之前包含当前
                                evaluate.click.value = true;
                                if (controller.evaluate.indexOf(e) == 0 && controller.clickCount1 == 1) {
                                  evaluate.click.value = false;
                                  controller.clickCount1 = 0;
                                } else if (controller.evaluate.indexOf(e) == 0 && controller.clickCount1 == 0) {
                                  controller.clickCount1++;
                                } else {
                                  controller.clickCount1 = 0;
                                }
                              }
                            }
                          }))
                      .toList(),
                )),
            const Spacer(),
            Container(
              padding: EdgeInsets.only(left: 17.w, right: 17.w, top: 1.w, bottom: 1.w),
              decoration: BoxDecoration(
                color: const Color(0xFF5C6DFF),
                borderRadius: BorderRadius.circular(60.r),
              ),
              child: Text(
                '评价',
                style: TextStyle(
                  color: Colors.white,
                  fontWeight: FontWeight.w400,
                  fontSize: 14.sp,
                ),
              ),
            ).clickOnce(controller.evaluateInfo)
          ],
        ),
        AppGaps.vGap10,
        CustomEvaluateInputPage(
          isShowTitle: false,
          text: '',
          hintText: '请输入评价',
          onCallBack: (value) {
            controller.userAddComment = value;
          },
          isRequired: false,
          isShowDivider: false,
        ),
      ],
    );
  }

  Widget _bottomWidget() {
    return Expanded(
        child: CustomEasyRefreshList<EducationCommentListEntity>(
      key: controller.globalKey,
      enableLoad: false,
      dataLoad: controller.listComment,
      itemBuilder: (item, index) {
        return itemWidget(item);
      },
      separated: Container(
        color: const Color(0xFFA5A9C8).withOpacity(0.2),
        height: 1.w,
      ),
    ));
  }
}
